package com.winexpress.account;

import com.winexpress.util.Log;
import com.winexpress.util.SessionAttributes;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

@WebServlet(name = "FacebookCallBack", urlPatterns = {"/facebookCallBack"})
public class FacebookCallBack extends HttpServlet {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WineXpress-war/AccountManagementWS.wsdl")
    private AccountManagementWS_Service service;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception_Exception {
        Facebook facebook = (Facebook) request.getSession().getAttribute(SessionAttributes.FACEBOOK);
        String oauthCode = request.getParameter("code");
        try {
            facebook.getOAuthAccessToken(oauthCode);
            HttpSession session = request.getSession();
            String firstName = facebook.getName().split(" ")[0];
            String lastName = facebook.getName().split(" ")[1];
            Customer customer = getFacebookCustomerAccount(facebook.getId(), firstName, lastName);
            if(!customer.isActivated()){
                session.setAttribute(SessionAttributes.ACCOUNT_DEACTIVATED, true);
                response.sendRedirect("/WineXpressClient/loginFail.jsp");
                return;
            }
            session.setAttribute(SessionAttributes.IS_ADMIN, false);
            session.setAttribute(SessionAttributes.USER_ID, customer.getId().toString());
            session.setAttribute(SessionAttributes.USER_FIRST_NAME, customer.getFirstName());
            session.setAttribute(SessionAttributes.USER_LAST_NAME, customer.getLastName());
            session.setAttribute(SessionAttributes.FACEBOOK_ID, customer.getFacebookID());
            Log.info("Facebook user = " + SessionAttributes.USER_ID+ " Log in");
        } catch (FacebookException e) {
            throw new ServletException(e);
        }
        response.sendRedirect(request.getContextPath() + "/initialization/index");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(FacebookCallBack.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(FacebookCallBack.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private Customer getFacebookCustomerAccount(java.lang.String facebookId, java.lang.String firstName, java.lang.String lastName) throws Exception_Exception {
        com.winexpress.account.AccountManagementWS port = service.getAccountManagementWSPort();
        Customer fbUser = new Customer();
        fbUser.setFacebookID(facebookId);
        fbUser.setFirstName(firstName);
        fbUser.setLastName(lastName);
        return port.getFacebookCustomerAccount(fbUser);
    }

}
